package com.shengqiang.cloud.ups.service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.shengqiang.cloud.ups.common.model.JsonResult;
import com.shengqiang.cloud.ups.dao.modal.UpmsRolePermission;
import com.shengqiang.cloud.ups.dao.repository.master.UpmsRolePermissionMapper;
import io.swagger.models.auth.In;
import org.apache.ibatis.plugin.Interceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/15.
 */
@Service
public class UpmsRolePermissionWebService extends ServiceImpl<UpmsRolePermissionMapper,UpmsRolePermission>{

    private static final Logger logger= LoggerFactory.getLogger(UpmsRolePermissionWebService.class);

    @Transactional
    public JsonResult assginRolePermission(Map<String,Object> params){
        JsonResult result=null;
        try{
            int roleId= Integer.parseInt(params.get("roleId").toString());
            String systemId=params.get("systemId").toString();
            List<Integer> permissions=(List<Integer>) params.get("permissions");
            int delTotal=baseMapper.deletePermissionByRoleId(roleId);
            List<UpmsRolePermission> rolePermissions= Lists.newArrayList();
            if(permissions!=null){
                for (int permissionId:permissions) {
                    UpmsRolePermission rolePermission=new UpmsRolePermission();
                    rolePermission.setRoleId(roleId);
                    rolePermission.setPermissionId(permissionId);
                    rolePermission.setSystemId(systemId);
                    rolePermissions.add(rolePermission);
                }

                int addResult=baseMapper.addRolePermissions(rolePermissions);
                if(addResult>1){
                    result=new JsonResult(0,"角色权限分配成功");
                }else{
                    result=new JsonResult(0,"角色权限分配失败");
                }
            }else{
                result= new JsonResult(1,"分配角色信息参数错误");
            }
        }catch (Exception ex){
            logger.error("分配角色信息出现异常:{}",ex.getMessage(),ex);
            result=new JsonResult(1,"角色权限信息分配失败");
        }
        return result;
    }

    public JsonResult<List<UpmsRolePermission>> selectRolePermissions(Map<String,Object> params){
        JsonResult<List<UpmsRolePermission>> result=null;
        try{
            List<UpmsRolePermission> rolePermissions=baseMapper.selectRolePermissions(params);
            if(rolePermissions!=null){
                result=new JsonResult<>(0,"成功返回角色权限");
                result.setData(rolePermissions);
            }else{
                result=new JsonResult<>(1,"查询角色权限信息失败");
            }
        }catch (Exception ex){
            logger.error("查询角色权限信息出现异常:{}",ex.getMessage(),ex);
            result=new JsonResult<>(1,"查询角色权限信息失败");
        }

        return result;
    }
}
